(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dataInquiry.runController', runController);
	runController.$inject = ['$rootScope', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dataInquiryService', 'commonService' ];
	function runController($rootScope, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, dataInquiryService, commonService) {
		var ctrl = this, dataInquiryInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.inquiryRun = {};
		ctrl.inquiryRun.runParms = ctrl.inquiryRun.runParms || [];

		ctrl.serviceCodeOptions = {};
		ctrl.dinqFilebuilderIdOptions = {};
		ctrl.parmTypeOptions = {};
		
		ctrl.init = function() {
			var params = {
				dinquiryId : dataInquiryInfo.dinquiryId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.params.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.inquiryRun.runParms';
//			ctrl.sitesLookupOptions = dataInquiryService.getSitesLookupOptions();
//			ctrl.accessTypeLookupOptions = dataInquiryService.getAccessTypeLookupOptions();
//			ctrl.runTypeOptions = dataInquiryService.getRunTypeOptions();
//			ctrl.allowQuoteFlagOptions = dataInquiryService.getAllowQuoteFlagOptions();
			$rootScope.initAuditOrNoteParams("","");
			authService.run(params).then(function(response) {
				ctrl.inquiryRun = response.data || dataInquiryInfo;
				ctrl.inquiryRun.runParms = ctrl.inquiryRun.runParms || [];
				angular.extend(ctrl.serviceCodeOptions,  response.data.services);
				angular.extend(ctrl.dinqFilebuilderIdOptions,  response.data.dataInquiryBuilders);
				angular.extend(ctrl.parmTypeOptions,  response.data.dataTypes);
			});
		};
		
		ctrl.checkParmVaule = function (){
			var m = 0;
			var values = [];
			ctrl.inquiryRun.runParms = ctrl.inquiryRun.runParms || [];
			if (ctrl.inquiryRun.runParms){				
				angular.forEach(ctrl.inquiryRun.runParms, function (value, key){
					if (!value.paramValue ) {
						return ;
					} else {					
						this.push(value.paramValue);
						m++;
					}
				},values); 
				return values;
			}
		};

		ctrl.runDownload = function() {
			var values = ctrl.checkParmVaule();
			if ( ctrl.inquiryRun.runParms.length === values.length){
				ctrl.inquiryRun.isTemporary = 'N';
				commonService.download(ctrl.inquiryRun, 'do/cda/dataInquiry/runDownload');
			} else {
				modalService.openErrorDialogs({
					'messages' : [ "js.pub.error.param-value-required"]
				});
			}
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedParamList = selectedRows;
		}

		function runDownloadSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'Run & Download successful' ]
			});
			ctrl.dataInquiry = response.data.model;
			ctrl.dataInquiry.taskId = response.data.task.taskId;
			ctrl.dataInquiry.paramList = ctrl.dataInquiry.paramList || [];
		}

		function runDownloadFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);